package com.example.gameforum.entity;


import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Set;

//帖子
@Entity
public class Post {

    @Id
    @Column(name = "id", nullable = false)
    private int id;

    //主题图
    @Column(name = "pictrue")
    private String pictrue;

    //主题
    @Column(name = "title")
    private String title;

    //内容
    @Column(name="context")
    private String context;

    //时间
    @Column(name = "time")
    private Date Time;

    //评论数
    @Column(name = "likenum")
    private int likenum;

    //用户多对一
    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)//可选属性optional=false,表示author不能为空。
    @JoinColumn(name="user_id")//设置在article表中的关联字段(外键)
    private User puser;

    //评论一对多
    @OneToMany(mappedBy = "cpost",cascade= CascadeType.ALL,fetch=FetchType.LAZY)
    private List<Comment> comments;

    //收藏多对多
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "favo_post", joinColumns = {
            @JoinColumn(name = "post_id", referencedColumnName = "id")}, inverseJoinColumns = {
            @JoinColumn(name = "favo_id", referencedColumnName = "id")})
    @Fetch(FetchMode.SUBSELECT)
    private Set<Favorite> pfavorites;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPictrue() {
        return pictrue;
    }

    public void setPictrue(String pictrue) {
        this.pictrue = pictrue;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public Date getTime() {
        return Time;
    }

    public void setTime(Date time) {
        Time = time;
    }

    public int getLikenum() {
        return likenum;
    }

    public void setLikenum(int likenum) {
        this.likenum = likenum;
    }

    public User getPuser() {
        return puser;
    }

    public void setPuser(User puser) {
        this.puser = puser;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public Set<Favorite> getPfavorites() {
        return pfavorites;
    }

    public void setPfavorites(Set<Favorite> pfavorites) {
        this.pfavorites = pfavorites;
    }
}
